package org.eiahe.hr.interview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.interview.domain.InterviewBehaviorOptions;
import org.eiahe.hr.interview.domain.InterviewBehaviorQuestions;
import org.eiahe.hr.interview.domain.InterviewQuestion;
import org.eiahe.hr.interview.domain.InterviewQuestionBank;
import org.eiahe.hr.interview.domain.vo.InterviewBehaviorQuestionsVO;
import org.eiahe.hr.interview.domain.vo.InterviewQuestionVO;
import org.eiahe.hr.interview.service.InterviewBehaviorOptionsService;
import org.eiahe.hr.interview.service.InterviewQuestionBankService;
import org.eiahe.hr.interview.domain.dto.InterviewQuestionQueryDTO;
import org.eiahe.hr.interview.mapper.InterviewBehaviorQuestionsMapper;
import org.eiahe.hr.interview.service.InterviewBehaviorQuestionsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :zhangnn
 * @className :InterviewBehaviorQuestionsServiceImpl
 * @description: TODO
 * @date 2025-08-20 11:56:06
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InterviewBehaviorQuestionsServiceImpl extends ServiceImpl<InterviewBehaviorQuestionsMapper, InterviewBehaviorQuestions>
    implements InterviewBehaviorQuestionsService {

    private final InterviewBehaviorQuestionsMapper  behaviorQuestionsMapper;
    private final InterviewBehaviorOptionsService behaviorOptionsService;
    private final InterviewQuestionBankService bankService;

    /**
     * 分页查询
     * @param queryDTO
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<InterviewQuestionVO> selectBehaviorQuestions(InterviewQuestionQueryDTO queryDTO, PageQuery pageQuery) {
        LambdaQueryWrapper<InterviewBehaviorQuestions> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null != queryDTO.getBankId(),InterviewBehaviorQuestions::getBankId,queryDTO.getBankId());
        lqw.orderByAsc(InterviewBehaviorQuestions::getQuestionOrder);
        IPage<InterviewBehaviorQuestionsVO> interviewBehaviorQuestionsVOIPage = behaviorQuestionsMapper.selectVoPage(pageQuery.build(), lqw);
        List<InterviewBehaviorQuestionsVO> records = interviewBehaviorQuestionsVOIPage.getRecords();

        List<InterviewQuestionVO> vos = new ArrayList<>();
        for (InterviewBehaviorQuestionsVO record : records) {
            InterviewQuestionVO  vo = new InterviewQuestionVO();
            InterviewQuestionBank bank = bankService.getById(record.getBankId());
            vo.setBankName(bank.getBankName());
            vo.setId(record.getId());
            vo.setBankId(bank.getId());
            vo.setQuestionTitle(record.getQuestionText());
            //选项
            List<InterviewBehaviorOptions> listOptions = behaviorOptionsService.selectOptions(record.getId());
            vo.setOptionsList(listOptions);
            vos.add(vo);
        }
        IPage<InterviewQuestionVO> page = new Page<>();
        page.setRecords(vos);
        page.setSize(interviewBehaviorQuestionsVOIPage.getSize());
        page.setCurrent(interviewBehaviorQuestionsVOIPage.getCurrent());
        page.setTotal(interviewBehaviorQuestionsVOIPage.getTotal());
        return TableDataInfo.build(page);
    }

    /**
     * 查询所有的题目
     * @param bankId
     * @param level
     * @return
     */
    @Override
    public List<InterviewQuestion> selectBehaviorQuestionsList(Long bankId, Integer level) {
        List<InterviewBehaviorQuestions> behaviorQuestions = this.baseMapper.selectList(new QueryWrapper<InterviewBehaviorQuestions>()
            .eq("bank_id",bankId)
            .orderByAsc("question_order")
        );
        List<InterviewQuestion> questions = new ArrayList<>();
        for (InterviewBehaviorQuestions bq : behaviorQuestions) {
            InterviewQuestion  question = new InterviewQuestion();
            question.setId(bq.getId());
            question.setQuestionTitle(bq.getQuestionText());
            question.setQuestionContent(String.valueOf(bq.getQuestionOrder()));
            List<InterviewBehaviorOptions> optionsList = behaviorOptionsService.selectOptions(bq.getId());
            question.setOptionsList(optionsList);
            questions.add(question);
        }
        return questions;
    }
}
